package ceshi4;


import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class JsonClient {
    public static void main(String[] args) {
        String jsonFilePath = "ww/src/ceshi4/info.json"; // JSON文件路径

        try {
            BufferedReader reader = new BufferedReader(new FileReader(jsonFilePath));
            StringBuilder jsonString = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                jsonString.append(line);
            }
            reader.close();

            JsonParser jsonParser = new JsonParser();
            JsonObject jsonObject = jsonParser.parse(jsonString.toString()).getAsJsonObject();
            // 获取名为"servers"的JsonArray对象
            JsonArray jsonArray = jsonObject.getAsJsonArray("servers");

            for (JsonElement jsonElement : jsonArray) {
                // 获取每个JsonElement对象中的JsonObject对象
                JsonObject serverObject = jsonElement.getAsJsonObject();
                // 获取"serverUrl"的值
                String serverUrl = serverObject.get("serverUrl").getAsString();
                // 获取"filePath"的值
                String filePath = serverObject.get("filePath").getAsString();
                // 获取"email"的值
                String email = serverObject.get("email").getAsString();
                // 获取"syncFiles"的JsonArray对象
                JsonArray syncFilesArray = serverObject.getAsJsonArray("syncFiles");
                // 将JsonArray对象转换为字符串输出
                String syncFiles = syncFilesArray.toString();

                System.out.println("Server URL: " + serverUrl);
                System.out.println("File Path: " + filePath);
                System.out.println("Email: " + email);
                System.out.println("Sync Files: " + syncFiles);
                System.out.println();
                String SERVER_ADDRESS = serverUrl; // 服务端IP地址
                int SERVER_PORT = 8000; // 服务端端口号
                String FILE_PATH = filePath + syncFiles.substring(2,syncFiles.length()-2); // 要上传的文件路径
                System.out.println(SERVER_ADDRESS);
                System.out.println(FILE_PATH);
                InetAddress address = InetAddress.getByName(SERVER_ADDRESS);
                System.out.println();
                if(address.isReachable(5000)){
                    System.out.println(line+"能够访问");
                    try {
                        Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
                        File file = new File(FILE_PATH);
                        byte[] fileData = new byte[(int) file.length()];
                        FileInputStream inputStream = new FileInputStream(file);
                        inputStream.read(fileData);
                        inputStream.close();
                        OutputStream outputStream = socket.getOutputStream();
                        outputStream.write(fileData);
                        outputStream.flush();
                        outputStream.close();
                        socket.close();
                        System.out.println("File uploaded successfully.");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else{
                    System.out.println(line+"无法访问");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

